//随机搜索框内的图标ICON数组
random_css_icons = ['shengdanxuehua', 'shengdanxiaoxiong', 'shengdanlaorenmao', 'shengdanqiqiu', 'shengdanwazi',
	'shengdanmilu', 'shengdanlihe'
];

//初始化帖子数组
function initTies(){
	var ties = [];
	//上次获取帖子数据的时间,防止过期数据过多
	var last_update_tie = localStorage.getItem("tie_update_time");
	if (last_update_tie == undefined || Date.now() - last_update_tie >= 3600000 || localStorage.getItem("ties") ==
		undefined) {
		axios.get('https://zj.v.api.aa1.cn/api/baidu-rs/')
			.then(function(response) {
				var data = response.data;
				$.each(data.data, function(k, v) {
					var new_obj = {
						'id': Math.floor(Math.random() * 100000000) + 1,
						"title": v.title,
						'hot': v.hot,
						"time": 0,
						"img": v.pic,
						"describe": v.desc,
						'uid': "系统推送",
						'location': '系统本地',
						'log': []
					};
					ties.push(new_obj);
				})
				localStorage.setItem("ties", JSON.stringify(ties));
				ties = getLatestData();
				localStorage.setItem("ties", JSON.stringify(ties));
				localStorage.setItem("tie_update_time", Date.now());
				resolveData();
				addBanner();
			})
			.catch(function(error) {
				console.error(error);
			});
	}
}
var ties = JSON.parse(localStorage.getItem('ties'));

//初始化用户数组
function initUsers(){
	if (localStorage.getItem("users") == undefined) {
		var users = [{
				uid: '333333',
				username: '张三',
				password: '20030905',
				head_img: 'imgs/logo.png'
			},
			{
				uid: '44444444',
				username: '李四',
				password: '20030905',
				head_img: 'imgs/logo.png'
			}
		]
		localStorage.setItem('users', JSON.stringify(users));
	};
}
//获取用户数组
function getUsers(){
	return JSON.parse(localStorage.getItem('users'));
}
//自动登入
function AutoLogin() {
	let has_login = false;
	let user;
	$.each(users, function(k, v) {
		//从缓存中自动登录
		if ($.cookie('uid') !== undefined && $.cookie('pswd') !== undefined) {
			if (v.uid == $.cookie('uid') && v.password === $.cookie('pswd')) {
				has_login = true;
				user = v;
			}
		}
	})
	if (has_login == false) {
		return false;
	}else{
		return user;
	}
}


function getWeatherAndLocation(){
	axios.get('https://www.tianqiapi.com/api?version=v9&appid=23035354&appsecret=8YvlPNrz')
		.then(function(response) {
			var data = response.data;
			$(".mydate").eq(0).text(data.data[0].date);
			$(".temperature").eq(0).text(data.data[0].tem + "°C");
			$(".air").eq(0).text(data.data[0].air_level);
			$(".air_tip").eq(0).text(data.data[0].air_tips);
			$(".city").eq(0).text(data.city);
			user_now_city = data.city;
		})
		.catch(function(error) {
			console.error(error);
		});
}



function addHot(){
	ties = JSON.parse(localStorage.getItem("ties"));
	index = 0;
	var obj; 
	$.each(ties,function(k,v){
		if(v.id==getUrlValue('pid')){
			index=k;
			obj = v;
		}
	});
	obj.hot = parseInt(obj.hot)+1;
	ties.slice(index,1,obj);
	localStorage.setItem("ties",JSON.stringify(ties));
}

//最新
function getLatestData() {
	var ties = JSON.parse(localStorage.getItem('ties'));
	let $temp = ties;
	$temp.sort(function(a, b) {
		// 如果a的属性值大于b的属性值，返回1，否则返回-1  
		return a.time > b.time ? -1 : 1;
	});
	return $temp;
}

//最热
function getHotestData() {
	var ties = JSON.parse(localStorage.getItem('ties'));
	let $temp = ties.concat();
	$temp.sort(function(a, b) {
		// 如果a的属性值大于b的属性值，返回1，否则返回-1  
		return a.hot > b.hot ? -1 : 1;
	});
	return $temp;
}

//热点&最新
function getRecommandData() {
	var ties = JSON.parse(localStorage.getItem('ties'));
	let $temp = ties;
	$temp.sort(function(a, b) {
		// 如果a的属性值大于b的属性值，返回1，否则返回-1  
		return (a.hot > b.hot && a.time > b.time) ? -1 : 1;
	});
	return $temp;
}


//设置url的页面
function setTiePage(v) {
	window.location.href = window.location.origin + window.location.pathname + "?" + "pid" + "=" + getUrlValue('pid') + "&page=" + v;
}

//获取url内key为k的value值
function getUrlValue(k) {
	var url = window.location.href;
	var params = url.split('?')[1];
	if (params == undefined) {
		window.open("index.html?page=1&mode=new","_self");
		return true;
	}
	var keyValuePairs = params.split('&');
	var result = {};
	for (var i = 0; i < keyValuePairs.length; i++) {
		var pair = keyValuePairs[i].split('=');
		var key = decodeURIComponent(pair[0]);
		var value = decodeURIComponent(pair[1]);
		result[key] = value;
	}
	return result[k];
}

//设置首页页数
function setIndexPage(v) {
	window.location.href = window.location.origin + window.location.pathname + "?" + "page" + "=" + v + "&mode=" +
		getUrlValue('mode');
}

//设置首页排序模式
function setIndexMode(v) {
	window.location.href = window.location.origin + window.location.pathname + "?" + "page=" + getUrlValue('page') +
		"&" + "mode" + "=" + v;
}

//将时间戳转为日期字符串格式
function TimeToStr(timestamp) {
	dateString = new Date(timestamp).toLocaleString();
  return dateString;
};


//退出按钮清空cookie
$("#quit_user").click(function() {
	$.cookie("uid", null, {
		expires: -1,
		path: '/'
	});
	$.cookie("pswd", null, {
		expires: -1,
		path: '/'
	});
})

//滚动条
$(window).scroll(function() {
	if ($(document).scrollTop() > 10) {
		$("#search_container").css({
			'position': 'fixed',
			'z-index': 1000,
			'top': '0px'
		});
	} else {
		$("#search_container").css({
			'display': 'inline-block',
			'position': 'none'
		}).fadeIn();
	}
	if ($(document).scrollTop() >= 300) {
			$("#slide_buttons").css({
				'display':'block'
			});
		} else{
			$("#slide_buttons").css({
				'display':'none'
			});
		}

});





